package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpDeviceGroupService;
import com.ck.core.BaseController;
import com.ck.exception.ControllerException;
import com.ck.model.common.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 客户端-终端分组
 *
 * @author linqiang
 * @version v1.0
 * @date 2018-09-30 15:57
 */
@PermissionRequired
@Controller
@RequestMapping(value = "/client/device/group")
public class CpDeviceGroupClientController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CpDeviceGroupService deviceGroupService;

    /**
     * 节目投放到终端
     *
     * @param request       请求
     * @param deviceGroupId 分组ID(多个英文逗号隔开)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/treeToDevice", method = RequestMethod.POST)
    public Object treeToDevice(HttpServletRequest request, String deviceGroupId) {
        //获取用户信息
        User user = getSessionUser(request);
        if (user == null) {
            return new ControllerException("用户名为空，请联系管理员");
        }
        try {
            return deviceGroupService.treeToDevice(user, deviceGroupId);
        } catch (Exception e) {
            logger.error("CpDeviceGroupClientController deviceGroupTreeList 终端分组列表失败：" + e.getMessage());
            return new ControllerException("终端分组列表失败");
        }
    }

    /**
     * 节目投放到终端组
     *
     * @param request   请求
     * @param startTime 投放开始时间
     * @param endTime   投放结束时间
     * @return Object
     */
    @ResponseBody
    @RequestMapping(value = "/treeToGroup", method = RequestMethod.POST)
    public Object treeToGroup(HttpServletRequest request, String startTime, String endTime) {
        //获取用户信息
        User user = getSessionUser(request);
        if (user == null) {
            return new ControllerException("用户名为空，请联系管理员");
        }
        try {
            return deviceGroupService.treeToGroup(user, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpDeviceGroupClientController deviceGroupTreeList 终端分组列表失败：" + e.getMessage(), e);
            return new ControllerException("终端分组列表失败");
        }
    }

}
